﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailClient.Windows.Exceptions;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Model;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.StaticData;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace CharacterTrailClient.Binding
{
    public class DataBinder
    {
        /// <summary>
        /// 绑定工序
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="textEditProcessName"></param>
        public static void BindingProcess(LookUpEdit lookUpEdit,TextEdit textEditProcessName)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;

                lookUpEdit.EditValueChanged += (sender, e) =>
                {
                    var model = (Process)lookUpEdit.GetSelectedDataRow();
                    if (model != null)
                    {
                        if (lookUpEdit.DataBindings.Count > 0)
                        {
                            var type = lookUpEdit.DataBindings[0].DataSource.GetType();
                            var pi = type.GetProperty(textEditProcessName.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.ProcessName, null);
                            textEditProcessName.Text = model.ProcessName;
                        }
                        else
                        {
                            textEditProcessName.EditValue = model.ProcessName;
                        }
                    }
                };

                itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
                itemLookUpEdit.Properties.Columns.Clear();
                itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                               {
                                                                   new LookUpColumnInfo(
                                                                       "ProcessNo", "工序代码", 120),
                                                                   new LookUpColumnInfo(
                                                                       "ProcessName", "工序名称", 160)
                                                               });
                itemLookUpEdit.Properties.DisplayMember = "ProcessNo";
                itemLookUpEdit.Name = "LookUpEdit";
                itemLookUpEdit.Properties.ValueMember = "ProcessNo";
                itemLookUpEdit.Properties.NullText = "";
                itemLookUpEdit.Properties.PopupFormWidth = 300;

                itemLookUpEdit.Properties.DataSource = StaticObjects.GetProcessList();
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        /// <summary>
        /// 绑定工序新的
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="textEditProcessName"></param>
        public static void BindingProcedure(LookUpEdit lookUpEdit, TextEdit textEditProcessName)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;

                lookUpEdit.EditValueChanged += (sender, e) =>
                {
                    var model = (TProcedureModel)lookUpEdit.GetSelectedDataRow();
                    if (model != null)
                    {
                        if (lookUpEdit.DataBindings.Count > 0)
                        {
                            var type = lookUpEdit.DataBindings[0].DataSource.GetType();
                            var pi = type.GetProperty(textEditProcessName.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.ProcedureName, null);
                            textEditProcessName.Text = model.ProcedureName;
                        }
                        else
                        {
                            textEditProcessName.EditValue = model.ProcedureName;
                        }
                    }
                };

                itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
                itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                               {
                                                                   new LookUpColumnInfo(
                                                                       "ProcedureNo", "工序代码", 120),
                                                                   new LookUpColumnInfo(
                                                                       "ProcedureName", "工序名称", 160)
                                                               });
                itemLookUpEdit.Properties.DisplayMember = "ProcedureNo";
                itemLookUpEdit.Name = "LookUpEdit";
                itemLookUpEdit.Properties.ValueMember = "ProcedureNo";
                itemLookUpEdit.Properties.NullText = "";
                itemLookUpEdit.Properties.PopupFormWidth = 300;

                itemLookUpEdit.Properties.DataSource = StaticObjects.GetProcedureModelList();
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        public static void BindingBomMainItem(GridLookUpEdit lookUpEdit, TextEdit textEditItemId, TextEdit textEditName,TextEdit textEditVersion)
        {
            BindingBomMainItem(lookUpEdit);
            lookUpEdit.EditValueChanging += (sender, e) =>
            {
                //view中的model
                var model = (TBomMainModel)lookUpEdit.Properties.GetRowByKeyValue(e.NewValue);
                if (model != null)
                {
                    if (lookUpEdit.DataBindings.Count > 0)
                    {
                        var type = lookUpEdit.DataBindings[0].DataSource.GetType();

                        if (textEditItemId != null)
                        {
                            var pi = type.GetProperty(textEditItemId.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.ProductItemId, null);
                            textEditItemId.EditValue = model.ProductItemId;
                        }

                        if (textEditName != null)
                        {
                            var pi = type.GetProperty(textEditName.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.ProductItemName, null);
                            textEditName.EditValue = model.ProductItemName;
                        }

                        if (textEditVersion != null)
                        {
                            var pi = type.GetProperty(textEditVersion.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.ProductVersion, null);
                            textEditVersion.EditValue = model.ProductVersion;
                        }
                    }
                    else
                    {
                        if (textEditItemId != null)
                        {
                            textEditItemId.EditValue = model.ProductItemId;
                        }

                        if (textEditName != null)
                        {
                            textEditName.EditValue = model.ProductItemName;
                        }

                        if (textEditVersion != null)
                        {
                            textEditVersion.EditValue = model.ProductVersion;
                        }
                    }
                }
            };
        }

        public static void BindingBomMainItem(GridLookUpEdit lookUpEdit)
        {
            try
            {
                BindingBomMainItem(lookUpEdit, false);

                IBom bom = InstanceFactory.GetBom();
                lookUpEdit.Properties.DataSource = bom.GetBomMainModels();
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        public static void BindingBomMainItem2(GridLookUpEdit lookUpEdit, bool initDataSource)
        {
            BindingBomMainItem(lookUpEdit, initDataSource);
            GridView gridView = lookUpEdit.Properties.View;
            var gridColumn = new GridColumn
                                 {
                                     FieldName = "ProductItemNumberVersion",
                                     Name = "代码[版本号]",
                                     Caption = @"代码[版本号]",
                                     Width = 150,
                                     Visible = true,
                                     VisibleIndex = 0
                                 };
            gridView.Columns.Insert(0, gridColumn);
            lookUpEdit.Properties.DisplayMember = "ProductItemNumberVersion";
            lookUpEdit.Properties.Name = "repositoryItemLookUpEdit";
            lookUpEdit.Properties.ValueMember = "ProductItemNumberVersion";
            lookUpEdit.Properties.PopupFormWidth = 650;
        }

        public static void BindingBomMainItem(GridLookUpEdit lookUpEdit,bool initDataSource)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;
                itemLookUpEdit.Properties.PopupFormWidth = 500;
                var gridView = new GridView();
                gridView.Columns.AddRange(new[]
                                              {
                                                  new GridColumn
                                                      {
                                                          FieldName = "ProductItemId",
                                                          Name = "物料内码",
                                                          Caption = @"物料内码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 0
                                                      },
                                                   new GridColumn
                                                      {
                                                          FieldName = "ProductItemNumber",
                                                          Name = "物料代码",
                                                          Caption = @"物料代码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 1
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "ProductItemName",
                                                          Name = "物料名称",
                                                          Caption = @"物料名称",
                                                          Width = 160,
                                                          Visible = true,
                                                          VisibleIndex = 2
                                                      },
                                          
                                                  new GridColumn
                                                      {
                                                          FieldName = "ProductVersion",
                                                          Name = "版本",
                                                          Caption = @"版本",
                                                          Width = 60,
                                                          Visible = true,
                                                          VisibleIndex = 3
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "CheckStatusDesc",
                                                          Name = "审核情况",
                                                          Caption = @"审核情况",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 4
                                                      }
                                              });
                gridView.FocusRectStyle = DrawFocusRectStyle.RowFocus;
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
                gridView.OptionsView.ShowAutoFilterRow = true;
                gridView.OptionsView.ShowGroupPanel = false;
                itemLookUpEdit.Properties.View = gridView;
                itemLookUpEdit.Properties.DisplayMember = "ProductItemNumber";
                itemLookUpEdit.Properties.Name = "repositoryItemLookUpEdit";
                itemLookUpEdit.Properties.ValueMember = "ProductItemNumber";
                itemLookUpEdit.Properties.NullText = "";

                if (initDataSource)
                {
                    IBom bom = InstanceFactory.GetBom();
                    itemLookUpEdit.Properties.DataSource = bom.GetBomMainModels();
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        public static void BindingProductItem(GridLookUpEdit lookUpEdit, TextEdit textEditItemId, TextEdit textEditName)
        {
            BindingProductItem(lookUpEdit);

            lookUpEdit.EditValueChanging += (sender, e) =>
            {
                //view中的model
                var model = (VIcItemQueryModel)lookUpEdit.Properties.GetRowByKeyValue(e.NewValue);
                if (model != null)
                {
                    if (lookUpEdit.DataBindings.Count > 0)
                    {
                        var type = lookUpEdit.DataBindings[0].DataSource.GetType();

                        if (textEditItemId != null)
                        {
                            var pi = type.GetProperty(textEditItemId.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.FItemId, null);
                            textEditItemId.EditValue = model.FItemId;
                        }

                        if (textEditName != null)
                        {
                            var pi = type.GetProperty(textEditName.DataBindings[0].BindingMemberInfo.BindingMember);
                            pi.SetValue(lookUpEdit.DataBindings[0].DataSource, model.FName, null);
                            textEditName.EditValue = model.FName;
                        }
                    }
                    else
                    {
                        if (textEditItemId != null)
                        {
                            textEditItemId.EditValue = model.FItemId;
                        }

                        if (textEditName != null)
                        {
                            textEditName.EditValue = model.FName;
                        }
                    }
                }
            };
        }

        public static void BindingProductItem(GridLookUpEdit lookUpEdit)
        {
            try
            {
                var itemLookUpEdit = lookUpEdit;
                itemLookUpEdit.Properties.PopupFormWidth = 500;
                var gridView = new GridView();
                gridView.Columns.AddRange(new[]
                                              {
                                                  new GridColumn
                                                      {
                                                          FieldName = "FItemId",
                                                          Name = "物料内码",
                                                          Caption = @"物料内码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 0
                                                      },
                                                   new GridColumn
                                                      {
                                                          FieldName = "FNumber",
                                                          Name = "物料代码",
                                                          Caption = @"物料代码",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 1
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "FName",
                                                          Name = "物料名称",
                                                          Caption = @"物料名称",
                                                          Width = 160,
                                                          Visible = true,
                                                          VisibleIndex = 2
                                                      },
                                          
                                                  new GridColumn
                                                      {
                                                          FieldName = "FModel",
                                                          Name = "规格",
                                                          Caption = @"规格",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 3
                                                      },
                                                  new GridColumn
                                                      {
                                                          FieldName = "FErpClsID",
                                                          Name = "属性(1外购/2自制)",
                                                          Caption = @"属性(1外购/2自制)",
                                                          Width = 120,
                                                          Visible = true,
                                                          VisibleIndex = 4
                                                      }
                                              });
                gridView.FocusRectStyle = DrawFocusRectStyle.RowFocus;
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
                gridView.OptionsView.ShowAutoFilterRow = true;
                gridView.OptionsView.ShowGroupPanel = false;
                itemLookUpEdit.Properties.View = gridView;
                itemLookUpEdit.Properties.DisplayMember = "FNumber";
                itemLookUpEdit.Properties.Name = "repositoryItemLookUpEdit";
                itemLookUpEdit.Properties.ValueMember = "FNumber";
                itemLookUpEdit.Properties.NullText = "";

                //var dataSourceList = StaticObjects.GetICItemModels();
                //var list = new List<tICItemModel>();
                //list.Insert(0, new tICItemModel { FName = "", FNumber = "", FItemID = 0 });
                //foreach (var tIcItemModel in dataSourceList)
                //{
                //    list.Add(tIcItemModel);
                //}

                //itemLookUpEdit.Properties.DataSource = list;
                itemLookUpEdit.Properties.DataSource = StaticObjects.GetICItemModels();
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }
    }
}
